from opcode import opmap

class CALL_FUNCTION(object):

    def __init__(self, args, kwargs):
        self.args = args
        self.kwargs = kwargs
        
class RETURN_VALUE(object):

    pass
    
class LOAD_FAST(object):

    def __init__(self, number):
        self.number = number

def pad(number):
    if len(number) == 1:
        return number + chr(0)
    else:
        return number

class Assembler(object):

    def __init__(self):
        self.code = ''
        
    def append(self, op):
        if isinstance(op, CALL_FUNCTION):
            self.code += chr(opmap['CALL_FUNCTION'])
            number = chr(op.args + op.kwargs << 8)
            self.code += pad(number)
        
        elif isinstance(op, RETURN_VALUE):
            self.code += chr(opmap['RETURN_VALUE'])
        
        elif isinstance(op, LOAD_FAST):
            self.code += chr(opmap['LOAD_FAST'])
            number = chr(op.number)
            self.code += pad(number)
            

real_code = '|\x00\x00\x83\x00\x00S'

def f():
    def get():
        pass
        
    def set():
        pass
        
    def delete():
        pass
'''
a = Assembler()
a.append(LOAD_FAST(f.func_code.co_varnames.index('delete')))
a.append(CALL_FUNCTION(0, 0))
a.append(RETURN_VALUE())

import dis
print repr(a.code)
dis.dis(a.code)
'''
